package com.main.rentsystem.controller.user;

import com.main.rentsystem.pojo.dto.HousePageQueryDTO;
import com.main.rentsystem.pojo.dto.OrderHouseDTO;
import com.main.rentsystem.pojo.entity.House;
import com.main.rentsystem.pojo.entity.PageBean;
import com.main.rentsystem.pojo.entity.Result;
import com.main.rentsystem.pojo.vo.OrderHouseVO;
import com.main.rentsystem.service.HouseServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.main.rentsystem.controller.user.UserController.USER_ID;


@RestController
@Slf4j
@RequestMapping("/user/house")
public class HouseController {

    @Autowired
    private HouseServer houseServer;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/page")
    public PageBean page(HousePageQueryDTO housePageQueryDTO){

        String key = "house_"
                + String.valueOf(housePageQueryDTO.getPage())
                + String.valueOf(housePageQueryDTO.getPageSize());
        //查询redis中是否存在
        PageBean pageBean = null;
        if(housePageQueryDTO.getName() == null && housePageQueryDTO.getAddress() == null && housePageQueryDTO.getFeature() == null){
            pageBean = (PageBean) redisTemplate.opsForValue().get(key);
            if(pageBean != null){
                return pageBean;
            }
        }
        //redis不存在，查询数据库
        pageBean = houseServer.pageQuery(housePageQueryDTO);
        redisTemplate.opsForValue().set(key, pageBean);

        return pageBean;
    }

    @PostMapping("/order")
    public Result OrderHouse(@RequestBody House house){
        log.info("预约看房：{}",house);
        houseServer.OrderHouse(house);
        return Result.success();
    }

    @GetMapping("/getOrder")
    public Result GetOrderHouse(){
        log.info("查看预约看房记录");
        OrderHouseDTO orderHouseDTO = new OrderHouseDTO();
        orderHouseDTO.setUserId(USER_ID);
        List<OrderHouseVO> orderHouseVOList = houseServer.GetOrderHouse(orderHouseDTO);
        return Result.success(orderHouseVOList);
    }



}
